﻿var nowImg;
function Pic_Item_Click(w_) {
    $("body").css({ overflowY: "hidden" });
    PicShow_closer.show();
    document.getElementsByTagName('body')[0].scroll = "no";
    BlackBoard.css({ opacity: 0.95 }).fadeIn("fast");
    //
    var w = $(w_);
    nowImg = w;
    PicShow_Img
        .css({ width: w.width(), height: w.height() })
        .attr("src", w.attr("src")).attr("src_raw", w.attr("src").replace("/_thumbs", ""))
        .attr("data-width", w.attr("data-width")).attr("data-height", w.attr("data-height"));
    PicShow_PicBox.css({ marginTop: -w.height() / 2, marginLeft: -w.width() / 2 });
    PicShow_PicBox.show();
    resize_PicShow();
    imgReady("" + w.attr("src").replace("/_thumbs", "") + "",
        function () { },
        function () {
            if (PicShow_Img.attr("src_raw") == w.attr("src").replace("/_thumbs", "")) PicShow_Img.attr("src", this.src);

        },
        function () { }
        )
}

var Show_PicID = 0, Show_EventID = 0;
//点击Pic_Item
function Pic_Item_Click_(w_, picID_, eventID_) {
    $("body").css({ overflowY: "hidden" });
    PicShow_closer.show();
    document.getElementsByTagName('body')[0].scroll = "no";
    //
    var w = $(w_);
    Show_PicID = picID_;
    //Show_EventID = eventID_;
    var $img = w.find("img");
    var src = w.find("img").attr("src");
    BlackBoard.css({ opacity: 0.95 }).fadeIn("fast");
    PicShow_Img
        .css({ width: $img.width(), height: $img.height() })
        .attr("src", $img.attr("src_")).attr("src_raw", $img.attr("src_").replace("/_thumbs", ""))
        .attr("data-width", $img.attr("data-width")).attr("data-height", $img.attr("data-height"));
    PicShow_PicBox.css({ marginTop: -$img.height() / 2, marginLeft: -$img.width() / 2 });
    PicShow_PicBox.show();
    resize_PicShow();
    imgReady("" + $img.attr("src_").replace("/_thumbs", "") + "",
        function () { },
        function () {
            if (PicShow_Img.attr("src_raw") == $img.attr("src_").replace("/_thumbs", "")) PicShow_Img.attr("src", this.src);

        },
        function () { }
        )
}
//关闭
function PicShow_close() {
    PicShow_Info.html("");
    //PicShow_CommentList.find("ul").html("");
    PicShow_PicBox.hide();
    PicShow_closer.hide();
    ShowPre.add(ShowNext).hide();
    BlackBoard.fadeOut("fast");
    $("body").css({ overflowY: "auto" });
}
//尺寸和位置调整
function resize_PicShow() {
    if (PicShow_Img.is(":hidden")) return;
    var imgW = PicShow_Img.attr("data-width"), imgH = PicShow_Img.attr("data-height");
    var winW = $(window).width() - 350, winH = $(window).height();
    //如果下边要放一排的话就winH = $(window).height() - 一排的高度
    var imgW2 = imgW, imgH2 = imgH;
    if (imgW / imgH > winW / winH) {
        if (imgW > winW) {
            imgW2 = winW;
            imgH2 = imgH * imgW2 / imgW;
        }
    } else {
        if (imgH > winH) {
            imgH2 = winH;
            imgW2 = imgH2 * imgW / imgH;
        }
    }
    PicShow_Img.animate({ width: imgW2, height: imgH2 }, ShowPre.is(":hidden") ? "normal" : 0, function () {
        
        ShowPre.add(ShowNext).show();
        ChkShowPreNext();
    });
    PicShow_PicBox.animate({ marginTop: -imgH2 / 2, marginLeft:  - imgW2 / 2 }, ShowPre.is(":hidden") ? "normal" : 0);
}
function Get_PicID_PreNext(n) {
    for (var i = 0; i < ListPic.length; i++) {
        if (Show_PicID == ListPic[i].ModelPic.ID) {
            var index2 = accAdd(i, n);
            if (index2 == -1 || index2 == ListPic.length) return 0;
            else return ListPic[index2].ModelPic.ID
        }
    }
}
function Get_Pic_PreNext(n) {
    var _ListPic = nowImg.parents("ul").find(".thumbnail img");
    for (var i = 0; i < _ListPic.length; i++) {
        if (nowImg.attr("src") == nowImg.parents("ul").find(".thumbnail img:eq(" + i + ")").attr("src")) {
            var index2 = accAdd(i, n);
            if (index2 == -1 || index2 == _ListPic.length) return null;
            else return _ListPic[index2];
        }
    }
}
function Get_PicIndex_PreNext(n) {
    for (var i = 0; i < ListPic.length; i++) {
        if (Show_PicID == ListPic[i].ModelPic.ID) {
            return accAdd(i, n);

        }
    }
}
//上一张，下一张
function ShowPreNext(n) {
    if (ShowPre.is(":hidden")) return;
    //PicShow_CommentList.find("ul").html("");

    //var picID2 = Get_PicID_PreNext(n);
    //var $Pic = $("#Pic" + picID2);
    //$Pic.find(".PicImg").click();
    //if (n == 1 && Get_PicIndex_PreNext(1) == ListPic.length - 1) {
    //    API_Pic_List_PreNext(n);
    //}

    var pic2 = Get_Pic_PreNext(n);
    if (pic2) pic2.click();
}
//上下按钮的显示
function ChkShowPreNext() {
    if (ShowPre.is(":hidden")) return;

    if (Get_Pic_PreNext(-1) != null) ShowPre.removeClass("fa-ban").removeClass("disabled").addClass("fa-chevron-left");
    else ShowPre.addClass("fa-ban").addClass("disabled").removeClass("fa-chevron-left");

    if (Get_Pic_PreNext(1) != null) ShowNext.removeClass("fa-ban").removeClass("disabled").addClass("fa-chevron-right");
    else ShowNext.addClass("fa-ban").addClass("disabled").removeClass("fa-chevron-right");
}
